<?php
/**
 * The Template for displaying all single posts.
 *
 * @package WordPress
 * @subpackage Twenty_Eleven
 * @since Twenty Eleven 1.0
 */

get_header(); ?>

		<div id="primary">
			<div id="content" role="main"><div>
				<?php 
				class custom_author {
					public $ID = '';
					public $name = '';
					public $web = '';
					public $bio = '';
					
					// custom fields
					public $avatar = '';
					public $background = '';
					public $banner = '';
					public $about = '';
					public $face = '';
				}
				$user = get_user_by( 'slug', get_query_var( 'author_name' ) );
				
				// get author informations
				$author = new custom_author();
				$author->ID = $user->ID;
				$author->name = $user->display_name;
				$author->web = $user->user_url;
				$author->bio = get_the_author_meta('description',$author->ID);
				
				
				$author->avatar = get_avatar($author->ID,32);
				$author->background = get_the_author_meta('background',$author->ID);
				$author->banner = get_the_author_meta('banner',$author->ID);
				$author->about = get_the_author_meta('about',$author->ID);
				$author->face = get_the_author_meta('face',$author->ID);
				
				// verify and show author information
				if ($author->background != '') {
					$url=getimagesize($author->background);
					if(is_array($url) != false)
					{
						?>
						<style type="text/css">
						body{background:url(<?php echo $author->background; ?>)}
						</style>
						<?php 
					}
				}
				
				if ($author->banner != '') :
					$url=getimagesize($author->banner);
					if(is_array($url) != false) :
				?>
						<div class="author-page-banner">
							<img src="<?php echo $author->banner; ?>" <?php echo $url[3];?> alt="author-page-banner"/>
						</div>
				
				<?php 
					endif;
				endif;
				if ($author->about == '') {
					$author->about = '<p>Hello my dear visitor! Please checkout my items as below and my information on the right side</p>';
				}
				?>
				<div class="author-page-about">
				<h2>Welcome</h2>
					<div class="about">
					<?php echo $author->about;?>
					</div>
				</div>
				<?php 
				
				if (strpos($author->face, 'facebook.com') === false) {
					$author->face = '';
				} else {
					if (strpos($author->face, 'http://') === false) {
						$author->face = 'http://'.$author->face;
					}
					if (strlen($author->face) <= strlen('http://www.facebook.com/')){
						$author->face = '';
					}
				}
				
				// loop to show posts by type
				$author_had_post = false;
				$post_types_full=get_post_types('','names');
				$post_type_exclude = 'page,attachment,revision,nav_menu_item,post,tutorial-items';
				$post_types = array();
				foreach ($post_types_full as $post_type ) {
					if (strpos($post_type_exclude,$post_type)===false) {
						$post_types[] = $post_type;
					}
				}
				?>
				<div class="author-items">
				<?php 
				for ($i = 0; $i < count($post_types); $i++):
					$args = array(
						'author' => $author->ID,
						'post_type' => $post_types[$i],
						'posts_per_page' => 1000
					);
					query_posts($args);
					$post_type_object = get_post_type_object($post_types[$i]);

					if ( have_posts() ) : 
						?>
						<div class="clear"></div>
						<h2><?php echo $post_type_object->labels->name;?></h2>
						<?php 
						while ( have_posts() ) : the_post();
						
						$post_id = get_the_ID();
						//$preview_image = get_post_meta($post_id, 'wpcf-preview-image', true);
						$preview_image = get_post_meta($post_id);
						$thumb = home_url().'/wp-includes/images/crystal/interactive.png';
						foreach ($preview_image as $value) {
							foreach ($value as $item) {
								if (strpos($item,home_url().'/wp-content/uploads/') !== false &&
										(	strpos(strtolower($item),'jpg') !== false ||
												strpos(strtolower($item),'png') !== false ||
												strpos(strtolower($item),'gif') !== false ||
												strpos(strtolower($item),'bmp') !== false) &&
										strlen($item) < strlen(home_url().'wp-content/uploads/2012/12/.jpg') + 50)
								{
									$thumb = timthumb($item);
									break;
								}
							}
						}

						
						?>
							<div class="item">
								<a href="<?php echo get_permalink();?>" class="thumb" title="<?php echo get_the_title();?>">
									<img src="<?php echo $thumb; ?>" alt="<?php echo get_the_title();?> thumb" width="180" height="150"/>
								</a>
								<h3 class="title">
									<a href="<?php echo get_permalink();?>" class="thumb" title="<?php echo get_the_title();?>">
										<?php the_title();?>
									</a>
								</h3>
							</div>
						<?php
						endwhile;  
					else:
					endif;
					rewind_posts();
				endfor;?>
				<div class="clear"></div>
				</div><div class="clear"></div>
			</div></div><!-- #content -->			
			
			<div id="side">
			
				<div class="widget">
					<?php 
					if (is_user_logged_in()) :
					$profile_page  = get_edit_profile_url( $author->ID );
					?>
						<a href="<?php echo $profile_page?>" class="author-edit">Edit your paged</a>
					<?php 
					endif;
					?>
					<a id="author-image" href="<?php echo $author->web; ?>"><?php echo $author->avatar; ?></a>				
					<h1 class="author-name"><?php echo $author->name; ?></h1>
					
					<p id="author-bio" class="author-desc"><?php echo $author->bio; ?></p>
					
					
					<?php if ($author->web != ''): ?>
						<h2 class="author-website">
							<a title="Author site" href="<?php echo $author->web;?>" class="author-web">
								<img src="<?php echo $author->web;?>/favicon.ico" alt="author-web-icon"/><?php echo $author->web;?>
							</a>
						</h2>
					<?php 
					endif;
					
					
					if ($author->face != '') : ?>
						<div class="space"></div>
						<h3>My friends</h3>
						<div class="fb-like-box" data-href="<?php echo $author->face;?>" data-width="260" data-show-faces="true" data-stream="false" data-border-color="white" data-header="false"></div>										
					<?php endif;?>
					
					
					<div class="space"></div>
					<h3>Share my page</h3>
					<div class="share">
						<!-- AddThis Button BEGIN -->
					<div class="addthis_toolbox addthis_counter_style" style="left:50px;top:50px;">
					<a class="addthis_button_facebook_like" fb:like:layout="box_count"></a>
					<a class="addthis_button_tweet" tw:count="vertical"></a>
					<a class="addthis_button_google_plusone" g:plusone:size="tall"></a>
					<a class="addthis_counter"></a>
					</div>
					<script type="text/javascript">var addthis_config = {"data_track_addressbar":false};</script>
					<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-4f98ab455ea4fbd4"></script>
					<!-- AddThis Button END -->
					</div>
					<!--Share-->
				</div>
			
				
				
			</div>
		</div><!-- #primary -->
		<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/single.js"></script>
<?php get_footer(); ?>